package com.kin.rich.service.run;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.kin.rich.pojo.Keyword;
import com.kin.rich.service.BaiduNewsSpider;
import com.kin.rich.service.SpiderHelper;

public class NewsSpiderRun {
	
	private SpiderHelper helper;
	private BaiduNewsSpider baiduNewsSpider;
	
	public void setHelper(SpiderHelper helper) {
		this.helper = helper;
	}

	public void setBaiduNewsSpider(BaiduNewsSpider baiduNewsSpider) {
		this.baiduNewsSpider = baiduNewsSpider;
	}


	public void runSpiderWithThread(){
		//baiduWebSpider.runSpiderWithThread();
		System.out.println("=========================");
	}
	
	public void runSpiderOneByOne(){
		List<Keyword> list = helper.getValidKeyword();
		//System.out.println(list);
		for(int i=0;i<list.size();i++){
			Keyword keyword = list.get(i);
			//if(keyword.getWordId()==35){
				System.out.println("=========="+keyword.getWordName());
			baiduNewsSpider.pageNum = 0;
			baiduNewsSpider.sameNews = 0;
			baiduNewsSpider.continueNews = false;
			//BaiduWebSpider spider = new BaiduWebSpider(list.get(i));
			//System.out.println(list.get(i).getWordName());
			baiduNewsSpider.Search(null,keyword);
			//baiduNewsSpider.Search("http://news.baidu.com/ns?from=news&cl=2&bt=1277913600&y0=2010&m0=7&d0=1&y1=2010&m1=8&d1=17&et=1282060799&q1=QDII%BB%F9%BD%F0&submit=%B0%D9%B6%C8%D2%BB%CF%C2&q3=&q4=&mt=0&lm=&s=2&begin_date=2010-7-1&end_date=2010-8-17&tn=newsdy&ct1=0&ct=0&rn=20&class=0&q6=", keyword);
			//baiduNewsSpider.Search("http://news.baidu.com/ns?from=news&cl=2&bt=1277913600&y0=2010&m0=7&d0=1&y1=2010&m1=7&d1=20&et=1279641599&q1=QDII%BB%F9%BD%F0&submit=%B0%D9%B6%C8%D2%BB%CF%C2&q3=&q4=&mt=0&lm=&s=2&begin_date=2010-7-1&end_date=2010-7-20&tn=newsdy&ct1=0&ct=0&rn=20&class=0&q6=", keyword);
			//baiduNewsSpider.Search("http://news.baidu.com/ns?from=news&cl=2&bt=1275321600&y0=2010&m0=6&d0=1&y1=2010&m1=7&d1=6&et=1278431999&q1=%BA%C3%C2%F2%BB%F9%BD%F0&submit=%B0%D9%B6%C8%D2%BB%CF%C2&q3=&q4=&mt=0&lm=&s=2&begin_date=2010-6-1&end_date=2010-7-6&tn=newsdy&ct1=0&ct=0&rn=20&class=0&q6=",keyword);
			//baiduNewsSpider.Search("http://news.baidu.com/ns?from=news&cl=2&bt=1270051200&y0=2010&m0=4&d0=1&y1=2010&m1=6&d1=1&et=1275407999&q1=%BA%C3%C2%F2%BB%F9%BD%F0&submit=%B0%D9%B6%C8%D2%BB%CF%C2&q3=&q4=&mt=0&lm=&s=2&begin_date=2010-4-1&end_date=2010-6-1&tn=newsdy&ct1=0&ct=0&rn=20&class=0&q6=", keyword);
			//baiduNewsSpider.Search("http://news.baidu.com/ns?from=news&cl=2&bt=1264953600&y0=2010&m0=2&d0=1&y1=2010&m1=4&d1=1&et=1270137599&q1=%BA%C3%C2%F2%BB%F9%BD%F0&submit=%B0%D9%B6%C8%D2%BB%CF%C2&q3=&q4=&mt=0&lm=&s=2&begin_date=2010-2-1&end_date=2010-4-1&tn=newsdy&ct1=0&ct=0&rn=20&class=0&q6=", keyword);
			//baiduNewsSpider.Search("http://news.baidu.com/ns?from=news&cl=2&bt=1262188800&y0=2009&m0=12&d0=31&y1=2010&m1=2&d1=1&et=1265039999&q1=%BA%C3%C2%F2%BB%F9%BD%F0&submit=%B0%D9%B6%C8%D2%BB%CF%C2&q3=&q4=&mt=0&lm=&s=2&begin_date=2009-12-31&end_date=2010-2-1&tn=newsdy&ct1=0&ct=0&rn=20&class=0&q6=", keyword);
			
			
			//}
			}
	}
	
	public static NewsSpiderRun getFromApplicationContext(ApplicationContext ctx) {
		return (NewsSpiderRun) ctx.getBean("NewsSpiderRun");
	}
	
	public static void main(String[] args) {
		ApplicationContext appContext = new FileSystemXmlApplicationContext("/src/applicationContext.xml");
		NewsSpiderRun spider =  NewsSpiderRun.getFromApplicationContext(appContext);
		spider.runSpiderOneByOne();
		//spider.runSpiderWithThread();
	}
}
